using System;
using UnityEngine;

namespace ProceduralPrimitivesUtil
{
	public class Sphere : PPBase
	{
		[Header("Basic parameters")]
		public float radius = 0.5f;

		[Header("Segments")]
		public int segments = 24;

		[Header("Slice")]
		public bool sliceOn;

		public float sliceFrom;

		public float sliceTo = 360f;

		public SphereCutOption cutTpye;

		public float cutFrom;

		public float cutTo = 1f;

		[Header("Mapping Coordinates")]
		public bool generateMappingCoords = true;

		public bool realWorldMapSize;

		public Vector2 UVOffset = new Vector2(0f, 0f);

		public Vector2 UVTiling = new Vector2(1f, 1f);

		[Header("Others")]
		public bool flipNormals;

		public bool smooth = true;

		protected override void CreateMesh()
		{
			m_mesh.name = "Sphere";
			radius = Mathf.Clamp(radius, 1E-05f, 10000f);
			segments = Mathf.Clamp(segments, 4, 100);
			sliceFrom = Mathf.Clamp(sliceFrom, 0f, 360f);
			sliceTo = Mathf.Clamp(sliceTo, sliceFrom, 360f);
			cutFrom = Mathf.Clamp01(cutFrom);
			cutTo = Mathf.Clamp(cutTo, cutFrom, 1f);
			float f = MathF.PI - Mathf.Acos((cutFrom - 0.5f) * 2f);
			float f2 = MathF.PI - Mathf.Acos((cutTo - 0.5f) * 2f);
			bool flag = cutTpye == SphereCutOption.HemiSphere;
			bool flag2 = cutTpye == SphereCutOption.SphericalSector;
			CreateSphere(pivotOffset + Vector3.zero, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius, segments, segments / 2, sliceOn, sliceFrom, sliceTo, flag || flag2, f, f2, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
			if (sliceOn)
			{
				Vector3 vector = new Vector3(Mathf.Sin(sliceFrom * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceFrom * (MathF.PI / 180f))) * radius * 0.5f;
				Vector3 vector2 = new Vector3(Mathf.Sin(sliceTo * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceTo * (MathF.PI / 180f))) * radius * 0.5f;
				CreateHemiCircle(pivotOffset + Vector3.zero, m_rotation * Vector3.up, m_rotation * vector.normalized, radius, segments / 2, 2, flag || flag2, f, f2, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, flag2);
				CreateHemiCircle(pivotOffset + Vector3.zero, m_rotation * Vector3.up, m_rotation * vector2.normalized, radius, segments / 2, 2, flag || flag2, f, f2, generateMappingCoords, realWorldMapSize, UVOffset, new Vector2(0f - UVTiling.x, UVTiling.y), !flipNormals, flag2);
			}
			Vector2 vector3 = new Vector2(Mathf.Sin(f), 0f - Mathf.Cos(f));
			Vector2 vector4 = new Vector2(Mathf.Sin(f2), 0f - Mathf.Cos(f2));
			if (flag)
			{
				CreateCircle(pivotOffset + m_rotation * new Vector3(0f, vector3.y * radius, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, radius * vector3.x, segments, 2, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals);
				CreateCircle(pivotOffset + m_rotation * new Vector3(0f, vector4.y * radius, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, radius * vector4.x, segments, 2, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
			}
			if (flag2)
			{
				float num = vector3.y * radius;
				float num2 = vector4.y * radius;
				if (cutFrom > 0.5f)
				{
					CreateCone(pivotOffset + m_rotation * new Vector3(0f, num * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, num, 0f, radius * vector3.x, segments, segments / 4, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
				}
				else
				{
					CreateCone(pivotOffset + m_rotation * new Vector3(0f, num * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, 0f - num, radius * vector3.x, 0f, segments, segments / 4, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals, smooth);
				}
				if (cutTo > 0.5f)
				{
					CreateCone(pivotOffset + m_rotation * new Vector3(0f, num2 * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, num2, 0f, radius * vector4.x, segments, segments / 4, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals, smooth);
				}
				else
				{
					CreateCone(pivotOffset + m_rotation * new Vector3(0f, num2 * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, 0f - num2, radius * vector4.x, 0f, segments, segments / 4, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
				}
			}
		}
	}
}
